<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//TODO redo login and registration forms done as configuration forms. 

class User extends MController {

	var $logged = false;

	function __construct ()
	{
		parent::__construct();
		$this->load->model('Usermodel');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->lang->load('user', 'Russian');
		$this->Usermodel->uid = (int)$this->session->userdata('user_id');
		$this->logged = $this->Usermodel->get();
	}

	function index ()
	{
		if ($this->Usermodel->uid) {
			redirect('/user/profile');
		} else {
			redirect('/user/login');
		}
		return ;
	}

	/**
	 * User login page.
	 */
	function login () {
		$data = array();
		if ($this->Usermodel->uid) {
			redirect('/user/profile');
		} elseif ($this->input->post('submit')) {
			$this->Usermodel->login = $this->input->post('login');
			$this->Usermodel->pass = $this->input->post('pass');
			$remember = $this->input->post('remember');
			if ($this->logged = $this->Usermodel->login()) {
				$this->session->set_userdata('user_id', $this->Usermodel->uid);
				
				$data['content'] = $this->load->view('greetings', '', true);
			} else {
				if (!$error = $this->session->flashdata('error')) {
					$this->session->set_flashdata('error', array('login_failed'));
				} else {
					$error[] = 'login_failed';
					$this->session->set_flashdata('error', $error);
				}
				redirect('/user/login');
			}
		} else {
			$this->lang->load('forms', 'Russian');
			$data['content'] = $this->load->view('login', '', true);
		}
		$data['general_menu'] = $this->load->view('main_menu', '', true);
		parent::display($data);
	}

	/**
	 * User registration page
	 */
	function registration () {
		if ($this->input->post('submit')) {
			if ($this->input->post('login') && $this->input->post('pass') && $this->input->post('email')) {
				$this->Usermodel->login = $this->input->post('login');
				$this->Usermodel->pass = $this->input->post('pass');
				$this->Usermodel->email = $this->input->post('email');
			}
			if ($user = $this->Usermodel->save()) {
				$data['center'] = $this->load->view('congratulation', '', true);
			} else {
				$data['center'] = $this->load->view('login_error', '', true);
			}
		} else {
			$this->lang->load('forms', 'Russian');
			
			$this->form_attribs = '';
			
			$this->items['login']->label = $this->lang->line('forms_lbl_login');
			$this->items['login']->require = true;
			$this->items['login']->error = '';
			$this->items['login']->input = form_input(array(
					'name'        => 'login',
					'id'          => 'login',
					'maxlength'   => '25',
					'size'        => '10'));
			
			$this->items['pass']->label = $this->lang->line('forms_lbl_pass');
			$this->items['pass']->require = true;
			$this->items['pass']->error = '';
			$this->items['pass']->input = form_password(array(
					'name'        => 'pass',
					'id'          => 'pass',
					'maxlength'   => '50',
					'size'        => '10'));
			
			$this->items['repeat_pass']->label = $this->lang->line('forms_lbl_repeat_pass');
			$this->items['repeat_pass']->require = true;
			$this->items['repeat_pass']->error = '';
			$this->items['repeat_pass']->input = form_password(array(
					'name'        => 'repeat_pass',
					'id'          => 'repeat_pass',
					'maxlength'   => '50',
					'size'        => '10'));
			
			$this->items['email']->label = $this->lang->line('forms_lbl_email');
			$this->items['email']->require = true;
			$this->items['email']->error = '';
			$this->items['email']->input = form_input(array(
					'name'        => 'email',
					'id'          => 'email',
					'maxlength'   => '100',
					'size'        => '10'));
			
			$this->items['submit']->input = form_submit('submit', $this->lang->line('forms_lbl_login_submit'));
			$this->items['submit']->error = false;
			
			$data['center'] = $this->load->view('regform', '', true);
		}
		
		$data['left_column'] = '';
		$data['right_column'] = '';
		$data['main_menu'] = $this->load->view('main_menu', '', true);
		$this->load->view('layout', $data);
	}

	/**
	 * User profile page.
	 */
	function profile () {
		// only logged in user can see this page. So rid out any log in checks 
		
		$this->Usermodel->get();
		$data = array('user' => $this->Usermodel, 'configs' => $this->getConfigs());
		$content = $this->load->view('profile', $data, true);
		parent::display($data);
	}
	/**
	 * Logout page.
	 */
	function logout () {
		$this->msmarty->assign('login', $this->Usermodel->login);
		$this->msmarty->assign('content', $this->msmarty->fetch('logout.html'));
		$this->session->sess_destroy();
		$this->msmarty->display('main.html');
	}

	/**
	 * User configuration page.
	 */
	function configuration () {
		redirect('/configuration/');
	}
	
	function getConfigs ($user_id = null) {
		$this->load->model('Configurationmodel');
		
		if (empty($user_id)) { $user_id = $this->Usermodel->uid; }
		
		$this->Configurationmodel->object_id = $user_id;
		$this->Configurationmodel->group = 'user';
		
//		TODO CHECK AND FIX THIS!
//		var_dump($this->Configurationmodel->get());
//		die;
		
		return $this->Configurationmodel->get();
	}
}

/* End of file user.php */
/* Location: ./system/application/controllers/user.php */
